# Setup Powerline
set-option -g status on
set-option -g status-interval 2
set-option -g status-justify "centre"
set-option -g status-left-length 60
set-option -g status-right-length 90
set-option -g status-left "#(~/.local/share/tmux-powerline/powerline.sh left)"
set-option -g status-right "#(~/.local/share/tmux-powerline/powerline.sh right)"
set-hook -g session-created 'run-shell "~/.local/share/tmux-powerline/powerline.sh init"' # prettifies the window-status segments

# Github colors for Tmux

set -g mode-style "fg=#c9d1d9,bg=#3b8eea"

set -g message-style "fg=#c9d1d9,bg=#3b8eea"
set -g message-command-style "fg=#c9d1d9,bg=#3b8eea"

set -g pane-border-style "fg=#444c56"
set -g pane-active-border-style "fg=#3b8eea"

set -g status "on"
set -g status-justify "left"

set -g status-style "fg=#3b8eea,bg=#1f2428"

set -g status-left-length "100"
set -g status-right-length "100"

set -g status-left-style NONE
set -g status-right-style NONE

set -g status-left "#[fg=#1f2428,bg=#3b8eea,bold] #S #[fg=#3b8eea,bg=#1f2428,nobold,nounderscore,noitalics]"
set -g status-right "#[fg=#1f2428,bg=#1f2428,nobold,nounderscore,noitalics]#[fg=#1f2428,bg=#1f2428] #{prefix_highlight} #[fg=#e1e4e8,bg=#1f2428,nobold,nounderscore,noitalics]#[fg=#24292e,bg=#e1e4e8] %Y-%m-%d  %I:%M %p #[fg=#3b8eea,bg=#e1e4e8,nobold,nounderscore,noitalics]#[fg=#1f2428,bg=#3b8eea,bold] #h "

setw -g window-status-activity-style "underscore,fg=#d1d5da,bg=#1f2428"
setw -g window-status-separator ""
setw -g window-status-style "NONE,fg=#24292e,bg=#1f2428"
setw -g window-status-format "#[fg=#1f2428,bg=#1f2428,nobold,nounderscore,noitalics]#[fg=#666666,bg=#1f2428,nobold,nounderscore,noitalics] #I  #W #F #[fg=#1f2428,bg=#1f2428,nobold,nounderscore,noitalics]"
setw -g window-status-current-format "#[fg=#1f2428,bg=#e1e4e8,nobold,nounderscore,noitalics]#[fg=#24292e,bg=#e1e4e8,bold] #I  #W #F #[fg=#e1e4e8,bg=#1f2428,nobold,nounderscore,noitalics]"

# Set history limit
set-option -g history-limit 100000

# 256 color terminal
set -g default-terminal "tmux-256color"
set-option -sa terminal-overrides ',*-256color:RGB'
set-option -sg escape-time 10

set -g base-index 1
setw -g pane-base-index 1

setw -g mode-keys vi

# Enable mouse scroll
setw -g mouse on

# Quick reload tmux config
bind-key r source-file ~/.config/tmux/tmux.conf \; display-message "Reloaded!"
bind-key M split-window -h "vim ~/.config/tmux/tmux.conf"

# Remap prefix key to 'C-space'
unbind C-b
set -g prefix C-Space
bind Space send-prefix

set-option -g focus-events on

# Load SSH_AUTH_SOCK for each pane
# set -g update-environment -r
set-environment -g 'SSH_AUTH_SOCK' ~/.ssh/ssh_auth_sock

# VIM like copy-paste
bind P paste-buffer
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection
bind-key -T copy-mode-vi r send-keys -X rectangle-toggle

# VIM like pane switch
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'

bind-key -T copy-mode-vi 'C-h' if -F '#{pane_at_left}' '' 'select-pane -L'
bind-key -T copy-mode-vi 'C-j' if -F '#{pane_at_bottom}' '' 'select-pane -D'
bind-key -T copy-mode-vi 'C-k' if -F '#{pane_at_top}' '' 'select-pane -U'
bind-key -T copy-mode-vi 'C-l' if -F '#{pane_at_right}' '' 'select-pane -R'

# Set new panes to open in current directory
bind c new-window -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

# Switch between windows with <TAB>
bind-key -r Tab next-window
bind-key -r BTab previous-window

#Zoom current window
bind-key -n 'M-o' resize-pane -Z 

#Resize panes
bind-key -n 'M-h' resize-pane -L
bind-key -n 'M-j' resize-pane -D
bind-key -n 'M-k' resize-pane -U
bind-key -n 'M-l' resize-pane -R

#Move window
bind-key -n C-S-Left swap-window -t -1\; select-window -t -1
bind-key -n C-S-Right swap-window -t +1\; select-window -t +1


# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'

### Plugin options
#Resurrect
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-processes 'k9s lf lg btm gopass journalctl'

run '~/.config/tmux/plugins/tpm/tpm'
